package com.example.dao.impl;

import com.example.dao.NoticeDao;
import com.example.model.Admin;
import com.example.model.Notice;
import com.example.model.Type;
import com.example.model.User;
import com.example.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class NoticeDaoImpl implements NoticeDao {

    @Override
    public List<Notice> getNoticesByCondition(Map<String, String> params) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Notice notice = null;
        int currentPage = Integer.parseInt(params.get("currentPage"));
        int pageSize = Integer.parseInt(params.get("pageSize"));
        int dataOff = (currentPage -1) * pageSize;
        List<Notice> noticeList = new ArrayList<>();
        try {
            conn = DBUtil.getConnection();
            String sql = "select id,b_type,b_time,title,content from tb_notice where 1=1  limit ?,?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,dataOff);
            ps.setInt(2,pageSize);
            rs = ps.executeQuery();
            while (rs.next()){
                notice = new Notice();
                notice.setId(rs.getInt("id"));
                notice.setBTime(rs.getDate("b_time"));
                notice.setBType(rs.getInt("b_type"));
                notice.setTitle(rs.getString("title"));
                notice.setContent(rs.getString("content"));
                noticeList.add(notice);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(conn,ps,rs);
        }
        return noticeList;
    }

    @Override
    public int getTotal(Map<String, String> params) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int total = 0;
        try {
            conn = DBUtil.getConnection();
            String sql = "select count(*) from tb_notice";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()){
                total = rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(conn,ps,rs);
        }
        return total;
    }

    @Override
    public int deleteById(int id) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = DBUtil.getConnection();
            String sql = "delete from tb_notice where id = ?";
            ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            return ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DBUtil.close(conn,ps,rs);
        }
        return -1;
    }
}
